package com.fubowen.lepton.helper

import android.content.Context
import android.graphics.Bitmap
import android.graphics.BitmapFactory
import android.graphics.Canvas
import android.graphics.Color
import android.graphics.Matrix
import android.graphics.Paint
import android.graphics.PorterDuff
import android.graphics.PorterDuffXfermode
import android.net.Uri
import com.fubowen.lepton.helper.ViewHelper.dpToPx
import java.io.BufferedOutputStream
import java.io.ByteArrayOutputStream
import java.io.File
import java.io.FileOutputStream
import java.io.InputStream

object ImageHelper {

    fun createEmptyImage(): InputStream {
        val bitmap = Bitmap.createBitmap(1, 1, Bitmap.Config.ARGB_8888)
        bitmap.eraseColor(Color.TRANSPARENT)
        val outputStream = ByteArrayOutputStream()
        bitmap.compress(Bitmap.CompressFormat.PNG, 100, outputStream)
        val byteArray = outputStream.toByteArray()
        return byteArray.inputStream()
    }

    fun fileToBitmap(context: Context, filePath: String): Bitmap {
        val file = File(filePath)
        val options = BitmapFactory.Options()
        options.inPurgeable = true
        options.inSampleSize = 2
        return BitmapFactory.decodeFile(filePath, options)
    }

    fun bitmapToFavicon(bitmap: Bitmap, filePath: String): String {
        val path = filePath.substringBeforeLast("/")
        File(path).let {
            if (!it.exists()) it.mkdirs()
            var buff: BufferedOutputStream? = null
            val file = File(filePath)
            kotlin.runCatching {
                buff = BufferedOutputStream(FileOutputStream(file))
                bitmap.compress(Bitmap.CompressFormat.JPEG, 100, buff!!)
            }.let {
                buff?.flush()
                buff?.close()
                return file.absolutePath
            }
        }
    }

    fun Context.bitmapToFavicon(bitmap: Bitmap?, url: String?): String? {
        if (bitmap == null || url.isNullOrEmpty()) {
            return null
        }
        val host = Uri.parse(url).host
        val path = this.externalCacheDir?.absolutePath + "/favicon/$host.jpg"
        val edge = this.dpToPx(16)
        return ImageHelper.bitmapToFavicon(bitmap.setSize(edge, edge), path)
    }
//    fun Context.bitmapToFile(bitmap: Bitmap,host:String): String {
//        Calendar.getInstance().apply {
//            val year = get(Calendar.YEAR)
//            val month = get(Calendar.MONTH)
//            val day = get(Calendar.DAY_OF_MONTH)
//            this.externalCacheDir.absolutePath+"/$year/$month/$day/"
//        }
//    }

    fun Bitmap.setSize(width: Int, height: Int): Bitmap {
        val w = this.width
        val h = this.height
        val sw = width.toFloat() / w
        val sh = height.toFloat() / h
        val matrix = Matrix()
        matrix.postScale(sw, sh)
        return Bitmap.createBitmap(this, 0, 0, w, h, matrix, true)
    }

    fun Bitmap.setCircle(): Bitmap {
        val width = this.width
        val paint = Paint()
        paint.isAntiAlias = true
        val bitmap = Bitmap.createBitmap(width, width, Bitmap.Config.ARGB_8888)
        val canvas = Canvas(bitmap)
        val r = width.toFloat() / 2
        canvas.drawCircle(r, r, r, paint)
        paint.xfermode = PorterDuffXfermode(PorterDuff.Mode.SRC_IN)
        canvas.drawBitmap(this, 0f, 0f, paint)
        return bitmap
    }
}